package com.nantai.jiafang.nantaijiafang.nt5s.fragments;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.nantai.jiafang.nantaijiafang.Data;
import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.RecyclerItemClickListener;
import com.nantai.jiafang.nantaijiafang.databinding.FragmentUperror2Binding;
import com.nantai.jiafang.nantaijiafang.nt5s.adapter.UperrorsearchAdapter;
import com.nantai.jiafang.nantaijiafang.nt5s.bean.Uperror_Item;
import com.nantai.jiafang.nantaijiafang.nt5s.fragments.ViewHolder.Holder_Fragment1;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/25.
 */

public class UpErrorfragment2 extends Fragment {
    private FragmentUperror2Binding binding;
    public static List<Uperror_Item> uperror_itemArrayList=new ArrayList<>();
    private Holder_Fragment1 holder_fragment1;
    private UperrorsearchAdapter uperrorsearchAdapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding= DataBindingUtil.inflate(inflater,R.layout.fragment_uperror2,container,false);
        holder_fragment1=new Holder_Fragment1(getContext().getApplicationContext());
        holder_fragment1.init(new Holder_Fragment1.Getdata() {
            @Override
            public void data(ArrayList<Uperror_Item> uperror_items) {
                uperror_itemArrayList=uperror_items;
                binding.modifyList.setLayoutManager(new LinearLayoutManager(getContext().getApplicationContext(), LinearLayoutManager.VERTICAL, false));
                uperrorsearchAdapter=new UperrorsearchAdapter(getContext().getApplicationContext(),getFragmentManager(), uperror_items);
                binding.modifyList.setAdapter(uperrorsearchAdapter);
            }
        });
        binding.modifyList.addOnItemTouchListener(new RecyclerItemClickListener(getContext().getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                    Data.MODE = position;
                    Data.uperror_item = uperror_itemArrayList.get(position);
                    getFragmentManager().beginTransaction()
                            .addToBackStack(null)                    //添加返回键返回
                            .setCustomAnimations(R.anim.in_from_left, R.anim.in_from_right, R.anim.out_from_left, R.anim.out_from_right)
                            .add(R.id.uperror_search, new UpErrorfragment_Add(), "fragment1")
                            .commit();

                }
            @Override
            public void onLongClick(View view, int posotion) {
                if (uperror_itemArrayList.get(posotion).getISOK().equals("未处理")) {
                    Data.MODE=posotion;
                    Data.uperror_item=uperror_itemArrayList.get(posotion);
                    if (Data.MODE!=-1){
                        getFragmentManager().beginTransaction()
                                .addToBackStack(null)					//添加返回键返回
                                .setCustomAnimations(R.anim.in_from_left,R.anim.in_from_right,R.anim.out_from_left,R.anim.out_from_right)
                                .add(R.id.uperror_search,new UpErrorfragment_Solve(),"fragment_solve")
                                .commit();
                    }
                }
            }
        }));
        return binding.getRoot();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Data.upErrorfragment2=this;
    }

    public void onModify(String predesc, int pos) {
        uperror_itemArrayList.get(pos).setProDesc(predesc);
        uperrorsearchAdapter.setprosec(predesc,pos);
    }
    public void onSlove(String analysis,String cause,String solve,String isok,int pos) {
        uperrorsearchAdapter.setsolve(analysis,cause,solve,isok,pos);
        uperror_itemArrayList.get(pos).setISOK(isok.equals("1")?"已处理":"未处理");
    }


}
